package com.Home.digital_life;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;

public class UpdateActivity extends Activity {
	String url = "192.168.1.100";
	int port = 5000;
	byte commond[] = new byte[2];
	DownFile downfile = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.update);

		Button buttonMusic = (Button) findViewById(R.id.update_music);
		Button buttonVideo = (Button) findViewById(R.id.update_video);

		buttonMusic.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				/*
				 * UpdateList update=new UpdateList(); //FileUtils fileutils=new
				 * FileUtils(); update.downFile("192.168.1.100:8080",new
				 * FileUtils().getSDPATH(), "music.xml");
				 */
				// update.XMLtoSQlite(path, filename);
				commond[0] = 1;
				commond[1] = 1;
				System.out.println("beforedownload");
				Upload.TcpRun(url, port, commond);
				System.out.println("download");
				downfile = Download.TcpRun(url, port);
				if (downfile.getCommond()[0] == 1
						&& downfile.getCommond()[1] == 1) {
					File file = new File(Environment
							.getExternalStorageDirectory() + "/music.xml");
					try {
						file.createNewFile();
						FileOutputStream fileOutput = new FileOutputStream(file);
						fileOutput.write(downfile.getStr());
						fileOutput.flush();
						fileOutput.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				}
			}
		});

		buttonVideo.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				/*
				 * UpdateList update=new UpdateList();
				 * update.downFile("192.168.1.100:8080",new
				 * FileUtils().getSDPATH(), "video.xml");
				 */
				// update.download("192.168.1.1:8080");
				// update.XMLtoSQlite(path, filename);
				commond[0] = 1;
				commond[1] = 2;
				Upload.TcpRun(url, port, commond);
				downfile = Download.TcpRun(url, port);
				if (downfile.getCommond()[0] == 1
						&& downfile.getCommond()[1] == 2) {
					File file = new File(Environment
							.getExternalStorageDirectory() + "/video.xml");
					try {
						file.createNewFile();
						FileOutputStream fileOutput = new FileOutputStream(file);
						fileOutput.write(downfile.getStr());
						fileOutput.flush();
						fileOutput.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		});

	}

}
